package org.shiki.hrm.service.impl;

import com.alibaba.fastjson.JSON;
import org.shiki.hrm.config.RabbitMQConfig;
import org.shiki.hrm.constant.CourseLevel;
import org.shiki.hrm.doc.CourseDoc;
import org.shiki.hrm.domain.Course;
import org.shiki.hrm.domain.CourseDetail;
import org.shiki.hrm.domain.CourseMarket;
import org.shiki.hrm.domain.CourseType;
import org.shiki.hrm.dto.CourseDto;
import org.shiki.hrm.feign.CourseDocFeignClient;
import org.shiki.hrm.mapper.CourseDetailMapper;
import org.shiki.hrm.mapper.CourseMapper;
import org.shiki.hrm.mapper.CourseMarketMapper;
import org.shiki.hrm.mapper.CourseTypeMapper;
import org.shiki.hrm.query.CourseDocQuery;
import org.shiki.hrm.service.ICourseService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.shiki.hrm.util.AjaxResult;
import org.shiki.hrm.vo.UserInfoVo;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 * @author shiki
 * @since 2022-03-12
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {
	@Autowired
	private CourseMapper courseMapper;

	@Autowired
	private CourseDetailMapper courseDetailMapper;

	@Autowired
	private CourseMarketMapper courseMarketMapper;

	@Autowired
	private CourseTypeMapper courseTypeMapper;

	@Autowired
	private CourseDocFeignClient courseDocFeignClient;

	@Autowired
	private RabbitTemplate rabbitTemplate;

	@Autowired
	private RedisTemplate redisTemplate;

	@Override
	public AjaxResult saveCourse(CourseDto dto) {
		String name = SecurityContextHolder.getContext().getAuthentication().getName();
		// 从redis中获取当前的用户信息
		Object o = redisTemplate.opsForValue().get("authen::" + name);
		UserInfoVo vo = JSON.parseObject(JSON.toJSONString(o), UserInfoVo.class);
		System.err.println(vo);
		Assert.notNull(dto, "参数不能为空!");
		Course course = dto.getCourse();
		// 先保存course
		course.setTenantName(vo.getCName());
		Long gradeId = course.getGradeId();
		String gradeName = course.getGradeName();
		CourseLevel level = CourseLevel.checkGrade(gradeId, gradeName);
		Assert.notNull(level, "课程等级不能为空!");
		courseMapper.insert(course);
		// 再保存courseDetail和courseMakert
		Long id = course.getId();
		CourseDetail courseDetail = dto.getCourseDetail();
		courseDetail.setId(id);
		courseDetailMapper.insert(courseDetail);
		CourseMarket courseMarket = dto.getCourseMarket();
		courseMarket.setId(id);
		courseMarketMapper.insert(courseMarket);
		return AjaxResult.me();
	}

	@Override
	public AjaxResult onLineCourse(List<Long> ids) {
		// 先把数据存入es
		List<Course> courses = courseMapper.selectBatchIds(ids);
		List<CourseDetail> courseDetails = courseDetailMapper.selectBatchIds(ids);
		List<CourseMarket> courseMarkets = courseMarketMapper.selectBatchIds(ids);
		List<Long> list = new ArrayList<>();
		courses.forEach(c ->{
			list.add(c.getTenantId());
		});
		List<CourseType> courseTypes = courseTypeMapper.selectBatchIds(list);
		List<CourseDoc> courseDocs = new ArrayList<>();
		for (int i = 0; i<= courses.size() -1; i++){
			CourseDoc courseDoc = new CourseDoc();
			BeanUtils.copyProperties(courses.get(i), courseDoc);
			BeanUtils.copyProperties(courseDetails.get(i), courseDoc);
			BeanUtils.copyProperties(courseMarkets.get(i), courseDoc);
			CourseType courseType = courseTypeMapper.selectById(courses.get(i).getCourseTypeId());
			courseDoc.setCourseTypeName(courseType.getName());
			courseDocs.add(courseDoc);
		}
		System.out.println(courseDocs + "===========>");
		AjaxResult ajaxResult = courseDocFeignClient.onLineCourse(courseDocs);
		// 判断是否保存成功
		Assert.isTrue(ajaxResult.isSuccess(), ajaxResult.getMessage());
		// 再修改数据库状态
		courses.stream().forEach(course -> {
			course.setStatus(1);
			courseMapper.updateById(course);
		});
		// 发送消息
		courses.stream().forEach(course -> {
			rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME_TOPIC, "message.sms", course.getName());
			rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME_TOPIC, "message.email", course.getName());
			rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME_TOPIC, "message.system", course.getName());
		});
		return AjaxResult.me();
	}

	@Override
	public AjaxResult offLineCourse(List<Long> ids) {
		// 先从es删除数据
		List<Course> courses = courseMapper.selectBatchIds(ids);
		List<CourseDetail> courseDetails = courseDetailMapper.selectBatchIds(ids);
		List<CourseMarket> courseMarkets = courseMarketMapper.selectBatchIds(ids);
		List<Long> list = new ArrayList<>();
		courses.forEach(c ->{
			list.add(c.getTenantId());
		});
		List<CourseDoc> courseDocs = new ArrayList<>();
		for (int i = 0; i<= courses.size() -1; i++){
			CourseDoc courseDoc = new CourseDoc();
			BeanUtils.copyProperties(courses.get(i), courseDoc);
			BeanUtils.copyProperties(courseDetails.get(i), courseDoc);
			BeanUtils.copyProperties(courseMarkets.get(i), courseDoc);
			CourseType courseType = courseTypeMapper.selectById(courses.get(i).getCourseTypeId());
			courseDoc.setCourseTypeName(courseType.getName());
			courseDocs.add(courseDoc);
		}
		AjaxResult ajaxResult = courseDocFeignClient.offLineCourse(courseDocs);
		Assert.isTrue(ajaxResult.isSuccess(), ajaxResult.getMessage());
		// 再改数据库状态
		courses.stream().forEach(course -> {
			course.setStatus(0);
			courseMapper.updateById(course);
		});
		return AjaxResult.me();
	}

	@Override
	public AjaxResult queryCourses(CourseDocQuery query) {
		AjaxResult ajaxResult = courseDocFeignClient.queryCourses(query);
		Assert.isTrue(ajaxResult.isSuccess(), "查询异常");
		return ajaxResult;
	}
}
